package com.xudow.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.base.QaExperts;
import com.activeshare.edu.ucenter.models.base.QaQuestions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.ui.task.qanda.ExpertAnsweredQuestionsTask;
import com.xudow.app.ui.task.qanda.ExpertDetailTask;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.Lists;
import com.xudow.app.util.Maps;

import org.w3c.dom.Text;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 专家详情Activty
 *
 * @author lukecheng
 */
public class ExpertDetailActivity extends BaseActivity {

    private long expertId;

    private LayoutInflater layoutInflater;

    private QaExperts expert;
    private List<QaQuestions> questions;

    private ExpertDetailTask expertDetailTask;
    private ExpertAnsweredQuestionsTask questionsTask;

    private CircleImageView portraitImageView;
    private TextView expertNameView;
    private TextView expertSubjectView;
    private TextView expertTitleView;
    private TextView expertDescView;
    private LinearLayout questionsLayout;
    private LinearLayout questionsAnsweredLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expert_detail);

        expertId = getIntent().getLongExtra("expertId", 0L);

        portraitImageView = (CircleImageView) findViewById(R.id.portrait);
        expertNameView = (TextView) findViewById(R.id.expert_name);
        expertSubjectView = (TextView) findViewById(R.id.export_subject);
        expertTitleView = (TextView) findViewById(R.id.export_title);
        expertDescView = (TextView) findViewById(R.id.description);
        questionsLayout = (LinearLayout) findViewById(R.id.questions);
        questionsAnsweredLayout = (LinearLayout) findViewById(R.id.questions_answered);

        layoutInflater = getLayoutInflater();

        questions = Lists.newArrayList();

        showLodingDialog(getString(R.string.loading));

        HashMap<String, String> params = Maps.newHashMap();
        params.put("expert_id", Long.toString(expertId));

        expertDetailTask = new ExpertDetailTask(this, expertDetailTaskHandler);
        addTask(expertDetailTask);
        expertDetailTask.execute(params); // 获取专家详细信息

        questionsTask = new ExpertAnsweredQuestionsTask(this, questionsTaskHandler);
        addTask(questionsTask);
        questionsTask.execute(params); // 获取专家回答过的的问题列表
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 获取数据后更新专家详细信息ui
     */
    private Handler expertDetailTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (ExpertDetailTask.SUCCESS == msg.what) {
                expert = (QaExperts) msg.getData().getSerializable("expert");

                if (null != expert) {
                    onExpertDetailSuccess();
                }
            }
        }
    };

    /**
     * 展示专家详细信息
     */
    private void onExpertDetailSuccess() {
        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, expert.getPhotoPath());
        ImageLoader.getInstance().displayImage(imageUrl, portraitImageView);

        expertNameView.setText(expert.getName());
        expertSubjectView.setText(expert.getSubject());
        expertTitleView.setText(expert.getTitle());
        expertDescView.setText(expert.getDescription());
    }

    /**
     * 获取数据后更新专家回答过的问题UI
     */
    private Handler questionsTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (ExpertAnsweredQuestionsTask.SUCCESS == msg.what) {
                QaQuestions[] dataArr = (QaQuestions[]) msg.getData().getSerializable("questions");

                if (null != dataArr && dataArr.length > 0) {
                    questions.addAll(Arrays.asList(dataArr));
                    createQuestionList();
                }
            }
        }
    };

    /**
     * 展示专家回答过得问题
     */
    private void createQuestionList() {
        if (null == questions || questions.size() <= 0) {
            questionsAnsweredLayout.setVisibility(View.GONE);
            return;
        }

        questionsAnsweredLayout.setVisibility(View.VISIBLE);

        for (int i = 0; i < questions.size(); i++) {
            QaQuestions question = questions.get(i);

            String sequence = String.format("%d、", i + 1);

            View view = layoutInflater.inflate(R.layout.item_answered_question_list, null);
            ((TextView) view.findViewById(R.id.sequence)).setText(sequence);
            ((TextView) view.findViewById(R.id.title)).setText(question.getQuestion());

            questionsLayout.addView(view);
        }
    }

    public void onAskClick(View view) {
        XApplication application = (XApplication) getApplication();
        if (!application.isLogin()) {
            String msg = getString(R.string.qanda_login_tip);
            Toast.makeText(ExpertDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(this, P2PQuestionActivity.class);
        intent.putExtra("expertId", expertId);
        startActivity(intent);
    }
}
